package main

import (
	"fmt"
	"reflect"
	"unsafe"
)

func main1() {
	v1 := uint(12)
	v2 := int(13)
	fmt.Println(reflect.TypeOf(v1)) // uint
	fmt.Println(reflect.TypeOf(v2)) // int

	fmt.Println(reflect.TypeOf(&v1)) // *uint
	fmt.Println(reflect.TypeOf(&v2)) // *int

	p := &v1
	p = (*uint)(unsafe.Pointer(&v2)) //使用unsafe.Pointer进行类型的转换

	fmt.Println(reflect.TypeOf(p)) // *uint
	fmt.Println(*p)                // 13
}

func main() {
	s := "hello world"
	b := string2bytes(s)
	fmt.Println(b)
	ss := bytes2string(b)
	fmt.Println(ss)
}

func string2bytes(s string) []byte {
	stringHeader := (*reflect.StringHeader)(unsafe.Pointer(&s))
	bh := reflect.SliceHeader{
		Data: stringHeader.Data,
		Len:  stringHeader.Len,
		Cap:  stringHeader.Len,
	}
	return *(*[]byte)(unsafe.Pointer(&bh))
}

func bytes2string(b []byte) string {
	sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b))
	sh := reflect.StringHeader{
		Data: sliceHeader.Data,
		Len:  sliceHeader.Len,
	}
	return *(*string)(unsafe.Pointer(&sh))
}
